import {
  INIT_BUYCART,
  ADD_CART,
  GET_USERINFO,
  RECORD_USERINFO,
  ADD_ANIMATION,
  SHOW_CART,
  REDUCE_CART,
  EDIT_CART
} from './mutation-types'
import { setStore } from '../utils/storage'
import { getCartList } from '@/api/cart'
export default {
  // 网页初始化时从本地缓存获取购物车数据
  [INIT_BUYCART](state) {
    getCartList().then(res => {
      state.cartList = res.data
    })
  },
  // 加入购物车
  [ADD_CART](state, { productId, productPrice, productName, productImg, productNum = 1 }) {
    const cart = state.cartList // 购物车
    let falg = true
    const goods = {
      productId,
      productPrice,
      productName,
      productImg
    }
    // 有内容
    if (cart.length) {
      cart.forEach(item => {
        if (item.productId === productId) {
          if (item.productNum >= 0) {
            falg = false
            item.productNum += productNum
          }
        }
      })
    }
    if (!cart.length || falg) {
      goods.productNum = productNum
      goods.checked = '1'
      cart.push(goods)
    }
    state.cartList = cart
  },
  // 加入购物车动画
  [ADD_ANIMATION](state, { moveShow, elLeft, elTop, img, cartPositionT, cartPositionL, receiveInCart }) {
    state.showMoveImg = moveShow
    if (elLeft) {
      state.elLeft = elLeft
      state.elTop = elTop
    }
    state.moveImgUrl = img
    state.receiveInCart = receiveInCart
    if (cartPositionT) {
      state.cartPositionT = cartPositionT
      state.cartPositionL = cartPositionL
    }
  },
  // 是否显示购物车
  [SHOW_CART](state, { showCart }) {
    state.showCart = showCart
  },
  // 移除商品
  [REDUCE_CART](state, { productId }) {
    const cart = state.cartList
    cart.forEach((item, i) => {
      if (item.productId === productId) {
        if (item.productNum > 1) {
          item.productNum--
        } else {
          cart.splice(i, 1)
        }
      }
    })
    state.cartList = cart
  },
  // 修改购物车
  [EDIT_CART](state, { productId, productNum, checked }) {
    const cart = state.cartList
    if (productNum) {
      cart.forEach((item, i) => {
        if (item.productId === productId) {
          item.productNum = productNum
          item.checked = checked
        }
      })
    } else if (productId) {
      cart.forEach((item, i) => {
        if (item.productId === productId) {
          cart.splice(i, 1)
        }
      })
    } else {
      cart.forEach((item) => {
        item.checked = checked
      })
    }
    state.cartList = cart
  },
  // 记录用户信息
  [RECORD_USERINFO](state, info) {
    // if (info.info.avator === undefined) {
    //   info.info.avator = state.userInfo.info.avator
    // }
    state.userInfo = info
    state.userId = info.info.id
    state.login = true
    setStore('userId', info.id)
    setStore('userInfo', info)
  },
  // 获取用户信息
  [GET_USERINFO](state, info) {
    if (state.userInfo && (state.userInfo.username !== info.username)) {
      return
    }
    if (!state.login) {
      return
    }
    if (!info.message) {
      state.userInfo = { ...info }
    } else {
      state.userInfo = null
    }
  },
  SET_LOGIN(state, login) {
    state.login = login
  },
  CLEAR_CART(state) {
    state.cartList = []
  }
}
